Code
#Data Collection
#Methods to analyze
#results #Data Collection
#Methods to analyze
#results #imports
import seaborn as sns
import pandas as pd
import numpy as np
from matplotlib import pyplot as plt
import altair as alt
import geopandas as gpd
import json
import folium
import pygris
import cenpy
import re
import copy
import warnings
import holoviews as hv
import hvplot.pandas
from holoviews import opts
hv.extension('bokeh')
import ipywidgets as widgets
from ipywidgets import interact
# Suppress all warnings
warnings.filterwarnings('ignore')/Users/lamhrb/anaconda3/lib/python3.11/site-packages/fuzzywuzzy/fuzz.py:11: UserWarning: Using slow pure-python SequenceMatcher. Install python-Levenshtein to remove this warning
warnings.warn('Using slow pure-python SequenceMatcher. Install python-Levenshtein to remove this warning')
#Check Parks
#read shape file
shape_file_loc = 'parks_map/geo_export_46a7de00-0067-42f5-a6bc-bec64e5a0f0b.shp'
#convert it into geopanda dataframe
def get_gpd_df(use_shape_file=True):
if use_shape_file:
gdf = gpd.read_file(shape_file_loc)
return gdf
parks_gpd = get_gpd_df()
#plot the map
park_map = parks_gpd.explore(
tiles="Cartodb positron",
style_kwds={
"weight": 2,
"color": "green",
"fillOpacity": 0.5,
}
)
folium.TileLayer(opacity=0.20).add_to(park_map)<folium.raster_layers.TileLayer at 0x14cdc00d0>
#create api conncection
ny_state_code = "36"
variables = ['NAME', 'B16008_002E', 'B16008_019E']
acs = cenpy.remote.APIConnection("ACSDT5Y2021")
NY_demo_data2021 = acs.query(
cols=variables,
geo_unit="tract:*",
geo_filter={"state": ny_state_code, "county": "*"},
)
acs2 = cenpy.remote.APIConnection("ACSDT5Y2010")
NY_demo_data2010 = acs2.query(
cols=variables,
geo_unit="tract:*",
geo_filter={"state": ny_state_code, "county": "*"},
)
NY_demo_data2010.dropna(inplace=True)
NY_demo_data2021.dropna(inplace=True)
#"061" Manhattan (New York County) 005 #Bronx 081 Queen 047 Brooklyn (Kings County) 085 Staten Island (Richmond County)
ny_city_counties = ["061" , "005" , "081", "047", "085"]
NY_demo_data2010 = NY_demo_data2010[NY_demo_data2010['county'].isin(ny_city_counties)]
print(len(NY_demo_data2010))
NY_demo_data2021 = NY_demo_data2021[NY_demo_data2021['county'].isin(ny_city_counties)]
for variable in variables:
if variable != "NAME":
NY_demo_data2010[variable] = NY_demo_data2010[variable].astype(float)
for variable in variables:
if variable != "NAME":
NY_demo_data2021[variable] = NY_demo_data2021[variable].astype(float)
NY_demo_data2010['population2010'] = NY_demo_data2010['B16008_002E'] + NY_demo_data2010['B16008_019E']
print(len(NY_demo_data2010[NY_demo_data2010['population2010']<0]))
NY_demo_data2010 = NY_demo_data2010[NY_demo_data2010['population2010']>0]
NY_demo_data2021['population2021'] = NY_demo_data2021['B16008_002E'] + NY_demo_data2021['B16008_019E']
NY_demo_data2021 = NY_demo_data2021[NY_demo_data2021['population2021']>0]
population_total_df = pd.merge(NY_demo_data2010, NY_demo_data2021 , on=['tract','NAME'])
population_total_df['change_precent'] = ((population_total_df['population2021'] - population_total_df['population2010']) /(np.absolute(population_total_df['population2010'])))*1002168
0
shape_file_loc = 'census_tract_shapefile/tl_2021_36_tract.shp'
#convert it into geopanda dataframe
def get_gpd_df(use_shape_file=True):
if use_shape_file:
gdf = gpd.read_file(shape_file_loc)
return gdf
nyc_gpd = get_gpd_df()
nyc_demo_merged = nyc_gpd.merge(
population_total_df,
left_on=["STATEFP", "COUNTYFP", "TRACTCE"],
right_on=["state_x", "county_x", "tract"],)
nyc_pct_change_mean = nyc_demo_merged['change_precent'].mean()nyc_demo_merged = nyc_demo_merged[['geometry', 'population2010','population2021','change_precent']]
def style(feature):
return {
'fillColor': 'green',
'color': 'black',
'weight': 2,
'fillOpacity': 0.6
}
m = nyc_demo_merged.explore(column="population2021",cmap = 'Blues',
tiles="CartoDB positron", zoom_start=11)
folium.Marker(location=[40.747993, -74.004890], popup="The High Line" , icon=folium.Icon(icon='tree' ,color='red')).add_to(m)
folium.Marker(location=[40.785091, -73.968285], popup="Central Park" , icon=folium.Icon(icon='tree' ,color='red')).add_to(m)
folium.Marker(location=[40.665535, -73.969749], popup="Prospect Park" , icon=folium.Icon(icon='tree' ,color='red')).add_to(m)
folium.Marker(location=[40.699215, -73.999039], popup="Brooklyn Bridge Park" , icon=folium.Icon(icon='tree' ,color='red')).add_to(m)
folium.Marker(location=[40.739716, -73.840782], popup="Flushing Meadows Corona Park" , icon=folium.Icon(icon='tree' ,color='red')).add_to(m)
folium.GeoJson(
parks_gpd,
name='geojson_layer',
style_function=style
).add_to(m)
m